package com.cloud.pole.shop.biz.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsSpec;
import com.cloud.pole.shop.model.entity.user.ShopUserDeliAddr;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.cloud.pole.mapper.user.ShopUserDeliAddrMapper;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopUserDeliAddrBiz extends BaseBiz<ShopUserDeliAddrMapper, ShopUserDeliAddr> {
	
	@Autowired
	ShopUserDeliAddrMapper shopUserDeliAddrMapper;
	
	@Cache(key="shopUserDeliAddr{1}")
    public ShopUserDeliAddr selectByPrimaryKey(Integer id) {
		 return super.selectById(id);
    }
	
	@Cache(key="shopUserDeliAddr:default{1}")
    public ShopUserDeliAddr selectDefaultUserDeliAddr(Long userId) {
		 ShopUserDeliAddr entity = new ShopUserDeliAddr();
		 entity.setUserId(userId);
		 entity.setDefaultTag(ShopUserDeliAddr.DEFAULT_TAG_YES);
		 return super.selectOne(entity);
    }
	
	@Override	
	@CacheClear(keys= {"shopUserDeliAddr{1.addrId}","shopUserDeliAddrList{1.userId}", "shopUserDeliAddr:default{1.userId}"})
    public void updateSelectiveById(ShopUserDeliAddr entity) {
		super.updateSelectiveById(entity);
	}
	
	@CacheClear(keys= {"shopUserDeliAddr{1.addrId}","shopUserDeliAddrList{1.userId}", "shopUserDeliAddr:default{1.userId}"})
	public void deleteByPrimaryKey(ShopUserDeliAddr entity) {
		super.deleteById(entity.getAddrId());
	}

	@CacheClear(key="shopUserDeliAddrList{1.userId}")
    public void insertUseGeneratedKeys(ShopUserDeliAddr entity) {
		shopUserDeliAddrMapper.insertUseGeneratedKeys(entity);
	}
	
	@Cache(key="shopUserDeliAddrList{1}")
    public List<ShopUserDeliAddr> selectListByUserId(Long userId) {
		 Example example = new Example(ShopUserDeliAddr.class);
		 Criteria criteria = example.createCriteria();
         example.setOrderByClause("default_tag desc");
		 criteria.andEqualTo("userId", userId);
		 return selectByExample(example);
    }
}
